﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace PositionMeasureSystem
{
    public class SensorData
    {
        public int IntPos = 0;
        public float Offset = 0;
        public float RateK = 0.0001F;
        public float aPos;
        public float rPos;
        public string Name;
        public void RefPos (int pos)
        {
            IntPos = pos;
            aPos = IntPos * RateK;
            rPos = (float)(IntPos + Offset) * RateK;
        }
        public void SetZero ()
        {
            Offset = -IntPos;
            rPos = 0;
        }
    }
    /*public class SamplingData_Double
    {
        public double time = 0;
        public double[] df;
        public uint index = 0;
        public double[,] SamplingBuf;
        const int BufLen = 16;
        public SamplingData_Double (int len)
        {
            df = new double[len];
            SamplingBuf = new double[len, 16];
        }
    }*/
    public class PosSensor_DLEN1
    {
        private string IPAddress = "192.168.0.3";
        public int TotalSensors = 0;
        private int Port = 64000;
        public int State = 0;
        public NetworkStream DLEN1NetworkStream;
        public TcpClient DLEN1Client = null;
        private System.Windows.Forms.Timer PosRdTimer = new System.Windows.Forms.Timer();
        public List<SensorData> Sensor = new List<SensorData>();
        public List<double> dPos = new List<double>();
        DateTime StartTime = new DateTime();
        public string[] DefualtSensorName = { "1", "2", "3", "4" };

        public int PortInit (string iP, int port)
        {
            int ret = 0;

            if (DLEN1Client == null)
            {
                DLEN1Client = new TcpClient();
            }

            if (DLEN1Client.Connected == false)
            {
                try
                {
                    IPAddress = iP; Port = port;
                    DLEN1Client.Connect(IPAddress, Port);

                    if (DLEN1Client.Connected == true)
                    {
                        StartTime = DateTime.Now;
                        DLEN1NetworkStream = DLEN1Client.GetStream();
                        byte[] buffer = System.Text.Encoding.Default.GetBytes("M0\r\n");
                        byte[] myReadBuffer;
                        DLEN1NetworkStream.Write(buffer, 0, buffer.Length);
                        myReadBuffer = new byte[256];
                        int len = DLEN1NetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length);
                        if (len > 0)
                        {
                            if (Sensor.Count > 0)
                            {
                                Sensor.Clear();
                            }
                            string str = System.Text.Encoding.Default.GetString(myReadBuffer);
                            str = str.Replace("\0", "");
                            string[] substr = str.Split(',');
                            TotalSensors = substr.Length - 1;

                            for (int i = 0; i < TotalSensors; i++)
                            {
                                SensorData sensor = new SensorData();
                                sensor.RefPos(Convert.ToInt32(substr[i + 1]));
                                sensor.Name = DefualtSensorName[Sensor.Count];
                                Sensor.Add(sensor);
                            }
                        }

                   }
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                    ret = -1;
                }
            }
            return ret;
        }
        public void ConnectReset ()
        {
            if (DLEN1Client != null)
            {
                DLEN1Client.Close();
                DLEN1Client.Dispose();
                DLEN1Client = null;
            }
            if (DLEN1NetworkStream != null)
            {
                DLEN1NetworkStream.Close();
                DLEN1NetworkStream.Dispose();
            }
        }
        int i = 0;
        public int SendM0Cmd ()
        {
            try
            {
                byte[] buffer = System.Text.Encoding.Default.GetBytes("M0\r\n");
                DLEN1NetworkStream.Write(buffer, 0, buffer.Length);
                //PosRdTimer.Interval = 50;
                i++;
            }
            catch
            {
                return -1;
            }
            return 0;
        }
        //string saveData;
        public int GetM0Data ()
        {
            int len = 0;
            if (SendM0Cmd() != 0)
                return -1;

            byte[] buff = new byte[256];
            try
            {
                len = DLEN1NetworkStream.Read(buff, 0, buff.Length);
            }
            catch
            {
                return -1;
            }
            if (len > 0)
            {
                string str = System.Text.Encoding.Default.GetString(buff, 0, len);
                //string str1;
                str = str.Replace("\0", "");
                //str1 = DateTime.Now + " " + str;
                string[] substr = str.Split(',');
                for (int i = 0; i < TotalSensors && i <= substr.Length; i++)
                {
                    Sensor[i].RefPos(Convert.ToInt32(substr[i + 1]));
                }               
            }
            return len;
        }

        public string GetM0String ()
        {
            PortInit(IPAddress, Port);
            byte[] buffer = System.Text.Encoding.Default.GetBytes("M0\r\n");
            byte[] myReadBuffer;
            DateTime dateTime = DateTime.Now;
            DLEN1NetworkStream.Write(buffer, 0, buffer.Length);
            myReadBuffer = new byte[256];
            DLEN1NetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length);
            long span = DateTime.Now.Ticks - dateTime.Ticks;
            string str = span.ToString() + "," + System.Text.Encoding.Default.GetString(myReadBuffer);
            return str = str.Replace("\0", "");
        }
        
    }
    class PosSensor
    {
    }
}
